package com.forever7776.life.admin.controller.life.m;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.forever7776.life.admin.controller.BaseController;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.sys.SysFunction;
import com.forever7776.life.core.service.sys.ISysFunctionService;
import com.forever7776.life.remote.common.enums.LevelEnum;
import com.forever7776.life.remote.common.vo.ResultVO;
import enums.ResultEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.ResultUtil;

/**
 * 菜单管理
 *
 * @author kz
 * @date 2018-01-15
 */
@Controller
@RequestMapping("/m/menu")
public class ManageMenuController extends BaseController {

    @Autowired
    private ISysFunctionService sysFunctionService;

    @RequestMapping
    public String index() {
        return LIFE_ADMIN_VIEW_PAGE_PATH + "menu/index";
    }

    /**
     * 查询菜单列表
     *
     * @param functionName
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam(value = "menuName", required = false) String functionName) {
        Page<SysFunction> page = sysFunctionService.selectAll(new Page<SysFunction>(pageNumber, pageSize), functionName);
        JSONObject result = new JSONObject();
        result.put("page", page);
        return result;
    }

    @RequestMapping("/{id}")
    @ResponseBody
    public JSONObject getById(@PathVariable String id) {
        SysFunction function = sysFunctionService.selectById(id);
        JSONObject result = ResultUtil.getSuccessResult("");
        result.put("menu", function);
        return result;
    }

    /**
     * 添加菜单
     *
     * @param function
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addRole(SysFunction function) {
        JSONObject result = checkForm(function, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        sysFunctionService.insert(function);
        return result;
    }

    /**
     * 添加子菜单
     *
     * @param function
     * @return
     */
    @RequestMapping(value = "/addChild/{parentId}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject addChildRole(@PathVariable String parentId, SysFunction function) {
        if (StringUtils.isEmpty(parentId) || sysFunctionService.selectById(parentId) == null) {
            return ResultUtil.getFailResult("该父级菜单不存在！");
        }
        JSONObject result = checkForm(function, "添加成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        function.setLevel(LevelEnum.NUM.second.getCode());
        function.setParentId(parentId);
        sysFunctionService.insert(function);
        return result;
    }

    /**
     * 添加菜单
     *
     * @param function
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateRole(@PathVariable String id, SysFunction function) {
        if (sysFunctionService.selectById(id) == null) {
            return ResultUtil.getFailResult("该菜单不存在！");
        }
        JSONObject result = checkForm(function, "编辑成功！");
        if (result.getInteger(ResultUtil.CODE).equals(ResultEnum.STATUS.FAIL.getCode())) {
            return result;
        }
        if (StringUtils.isEmpty(function.getIcon())) {
            function.setIcon(null);
        }
        sysFunctionService.updateById(function);
        return result;
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultVO deleteRole(@PathVariable String id) {
        boolean del = sysFunctionService.deleteById(id);
        if (del) {
            return ResultVoUtil.getSuccessResult("删除成功!");
        }
        return ResultVoUtil.getFailResult("删除失败~");
    }

    /**
     * 冻结菜单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/frozen/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject frozenRole(@PathVariable String id, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        sysFunctionService.frozen(id, status);
        return ResultUtil.getSuccessResult("操作成功！");
    }

    public JSONObject checkForm(SysFunction function, String msg) {
        if (function == null) {
            return ResultUtil.getFailResult("菜单为空");
        }
        if (StringUtils.isEmpty(function.getName())) {
            return ResultUtil.getFailResult("菜单名称为空");
        }
        if (StringUtils.isEmpty(function.getUrl())) {
            return ResultUtil.getFailResult("菜单路径为空");
        }
        return ResultUtil.getSuccessResult(msg);
    }
}
